#include "client.h"
#define MAXSIZE 1024
extern int pipefds[2];

void showmenu(void) {
    printf("*****************************\n");
    printf("*************cd**************\n");
    printf("*************ls**************\n");
    printf("************rmdir************\n");
    printf("************mkdir************\n");
    printf("*************pwd*************\n");
    printf("********puts filename********\n");
    printf("********gets filename********\n");
    printf("********rm filename**********\n");
    printf("************ quit ***********\n");
    printf("*****************************\n");
}//接收确定个字节的数据
int recvn(int sockfd, void* buff, int len) {
    int left = len;
    char* pbuf = (char*)buff;
    int ret = 0;
    while (left > 0) {
        ret = recv(sockfd, pbuf, left, 0);
        if (ret < 0) {
            perror("recv");
            break;
        } else if (ret == 0) {
            break;
        }
        pbuf += ret;
        left -= ret;
    }
    return len - left;
}

//发送确定个数据
int sendn(int sockfd, const void* buff, int len) {
    int left = len;
    const char* pbuf = (char*)buff;
    int ret = 0;
    while (left > 0) {
        ret = send(sockfd, pbuf, left, 0);
        if (ret == -1) {
            perror("send");
            return -1;
        }
        pbuf += ret;
        left -= ret;
    }
    return len - left;
}
// 解析命令行输入，分离命令和参数
void parseInput(char* input, char** args, char** content) {
    *args = strtok(input, " \n");
    *content = strtok(NULL," \n");    
}

int tcpinit(char *ip,char *port){
//创建客户端套接字
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if (clientfd == -1) {
        error(1, errno, "socket");
    }

    //服务端网络地址
    struct sockaddr_in serveraddr;
    socklen_t len = sizeof(serveraddr);
    memset(&serveraddr, 0, len);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(port));
    serveraddr.sin_addr.s_addr = inet_addr(ip);

    int ret = connect(clientfd, (const struct sockaddr*)&serveraddr, len);
    if (ret == -1) {
        error(1, errno, "connect");
    }
    return clientfd;
}

/*
void doTask(client_task_t * task){
        switch (task->train.type) {
        case CMD_TYPE_PWD :
            pwdCommand(task);
            break;
        case CMD_TYPE_LS:
            // 处理 ls 命令
            lsCommand(task);
            break;
        case CMD_TYPE_CD:
            // 处理 cd 命令
            printf("Changing directory ...\n");
            break;
        case CMD_TYPE_MKDIR:
            // 处理 mkdir 命令
            printf("Creating directory ...\n");
            break;
        case CMD_TYPE_RMDIR:
            // 处理 rmdir 命令
            printf("Removing directory ...\n");
            break;
        case CMD_TYPE_PUTS:
            sendn(task->pipefd,&task->train,8+task->train.len);
            printf("send %s over\n", task->train.buff);
            return;
        case CMD_TYPE_GETS:
            sendn(task->pipefd,&task->train,8+task->train.len);
            printf("Receiving file ...\n");
            return;
        case CMD_TYPE_TREE:
            // 处理 tree 命令
            treeCommand(task);
            printf("Receiving tree ...\n");
            break;
        case CMD_TYPE_QUIT:
            pthread_join(tid,NULL);
            break;
        default:
            // 可选：处理未预期的命令类型
            printf("Unhandled command type.\n");
            break;
        }
        task->train.len = strlen(task->train.buff);
        int ret = sendn(task->peerfd, &task->train, 4 + 4 + task->train.len);
        if (ret == -1) {//出错
            printf("sendn");
    }

}*/



